package com.goldsprite.gdxcore.assets;

import com.badlogic.gdx.graphics.*;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.ui.*;
import com.badlogic.gdx.scenes.scene2d.utils.Drawable;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;
import com.goldsprite.gdxcore.assets.utils.FontUtils;
import com.goldsprite.gdxcore.assets.utils.JarUtils;

public class UIAssets {
	//单例
	private static UIAssets instance;

	//静态路径
	public static String skinPath = "ui_skins/shade/skin/uiskin.json";

	//预设常量
	public static final int editorFontSize = 40;
	public static final float editorFontScl = 0.5f;
	public static final float loggerFontScl = 0.4f;
	public static final float editorSplitHandleSize = 25;//分割条宽度12
	public static final int editorWindowResizeBorderSize = 20;

	public Skin defaultSkin;
	public BitmapFont font;
	public BitmapFont smallFont;

	public Skin editorSkin;
	public BitmapFont editorFont, editorFontGray;


	public UIAssets() {
		instance = this;

		//材质

		//默认主题
		defaultSkin = new Skin(JarUtils.getActualFilesInternal(skinPath));
		font = FontUtils.generate();
		smallFont = FontUtils.generate(18);

		//编辑器主题
		{
			editorSkin = new Skin(JarUtils.getActualFilesInternal(UIAssets.skinPath));

			editorFont = FontUtils.generate(editorFontSize);
			editorFont.getData().setScale(editorFontScl);
			editorFontGray = FontUtils.generate(editorFontSize);
			editorFontGray.getData().setScale(editorFontScl);
			editorFontGray.setColor(Color.GRAY);

			BitmapFont loggerFont = FontUtils.generate(editorFontSize);
			loggerFont.getData().setScale(loggerFontScl);

			Label.LabelStyle labelStyle = editorSkin.get(Label.LabelStyle.class);
			labelStyle.font = editorFont;

			// 创建"small"样式
			Label.LabelStyle smallLabelStyle = new Label.LabelStyle();
			smallLabelStyle.font = smallFont;
			smallLabelStyle.fontColor = Color.WHITE;
			editorSkin.add("small", smallLabelStyle);

			Label.LabelStyle loggerLabelStyle = editorSkin.get(Label.LabelStyle.class);
			loggerLabelStyle.font = loggerFont;
			editorSkin.add("loggerLabelStyle", loggerLabelStyle);

			Label.LabelStyle titleLabelStyle = editorSkin.get("title", Label.LabelStyle.class);
			titleLabelStyle.font = editorFont;
			editorSkin.add("title", titleLabelStyle);

			Label.LabelStyle subtitleLabelStyle = editorSkin.get("subtitle", Label.LabelStyle.class);
			subtitleLabelStyle.font = smallFont;
			editorSkin.add("subtitle", subtitleLabelStyle);

			SelectBox.SelectBoxStyle selectBoxStyle = editorSkin.get(SelectBox.SelectBoxStyle.class);
			selectBoxStyle.font = smallFont;

			TextButton.TextButtonStyle textButtonStyle = editorSkin.get(TextButton.TextButtonStyle.class);
			textButtonStyle.font = editorFont;

			TextField.TextFieldStyle textFieldStyle = editorSkin.get(TextField.TextFieldStyle.class);
			textFieldStyle.font = editorFont;
			textFieldStyle.messageFont = editorFontGray;
			TextField.TextFieldStyle noBackgroundTextFieldStyle = new TextField.TextFieldStyle(textFieldStyle);
			noBackgroundTextFieldStyle.background = null; // 只修改新实例的背景
			editorSkin.add("nobackground", noBackgroundTextFieldStyle);

			//split style修改
			float splitBarThickness = 12;
			Pixmap pm = new Pixmap(1, 1, Pixmap.Format.RGBA8888);
			pm.setColor(new Color(0, 0, 0, 0.2f));
			pm.fill();
			TextureRegionDrawable cDrawable = new TextureRegionDrawable(new TextureRegion(new Texture(pm)));
			SplitPane.SplitPaneStyle splitPaneStyle = editorSkin.get("default-vertical", SplitPane.SplitPaneStyle.class);
			splitPaneStyle.handle = cDrawable;
			splitPaneStyle.handle.setMinHeight(splitBarThickness);
			SplitPane.SplitPaneStyle splitPaneStyleHori = editorSkin.get("default-horizontal", SplitPane.SplitPaneStyle.class);
			splitPaneStyleHori.handle = cDrawable;
			splitPaneStyleHori.handle.setMinWidth(splitBarThickness);

			Touchpad.TouchpadStyle touchpadStyle = editorSkin.get(Touchpad.TouchpadStyle.class);
			Drawable knob = touchpadStyle.knob;
			knob.setMinWidth(45);
			knob.setMinHeight(45);

			List.ListStyle listStyle = editorSkin.get(List.ListStyle.class);
			listStyle.font = editorFont;

			CheckBox.CheckBoxStyle checkBoxStyle = editorSkin.get("switch", CheckBox.CheckBoxStyle.class);
			checkBoxStyle.font = editorFont;

			// 复制默认样式并创建新样式
			ScrollPane.ScrollPaneStyle defaultStyle = editorSkin.get(ScrollPane.ScrollPaneStyle.class);
			ScrollPane.ScrollPaneStyle noBackgroundStyle = new ScrollPane.ScrollPaneStyle(defaultStyle);
			noBackgroundStyle.background = null; // 只修改新实例的背景
			editorSkin.add("nobackground", noBackgroundStyle);
		}
	}

	public static UIAssets getInstance() {
		if (instance == null) new UIAssets();
		return instance;
	}
}

